import java.util.ArrayList;
import java.util.List;

class Solution19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
         if(head == null ) return head ;
         ListNode cur = head ; 
         List<ListNode> list = new ArrayList<>() ;
         while(cur != null){
            list.add(cur) ; 
           cur = cur.next ;
         }
        int tar = list.size() -n+1 -1 ;
        if(tar <= 0){
            return list.get(0).next ;
        }
        ListNode prev = list.get(tar-1) ; 
        prev.next = prev.next.next ;
        return head ;
    }
}